﻿using MediatR;
using Messhall.Application.Dto;
using Messhall.Application.Dto.Permissions.Requests;
using Messhall.Application.Dto.Permissions.Responses;
using Messhall.Application.Dto.Users.Requests;
using Messhall.Application.Permission.Commands;
using Messhall.Application.Permission.Commands.Permission;
using Messhall.Application.Permission.Services;
using Messhall.HostService.Interface.Permission;
using Microsoft.AspNetCore.Mvc;

namespace Messhall.HostService.Controllers.Permission;

[Route("api/[controller]")]
[ApiController]
public class PermissionController : ControllerBase, IPermission
{
    private readonly IMediator _mediator;

    private readonly PermissionAppService _permissionAppService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="mediator"></param>
    /// <param name="usersAppService"></param>
    public PermissionController(IMediator mediator, PermissionAppService permissionAppService)
    {
        this._mediator = mediator;
        this._permissionAppService = permissionAppService;
    }
    
    /// <summary>
    /// 获取详情
    /// </summary>
    [HttpGet]
    [Route("get")]
    public async Task<PermissionDetailsResponse> GetDetailsAsync([FromQuery]RequestKeyIdBase request)
    {
        return await _permissionAppService.GetDetailsAsync(request);
    }

    /// <summary>
    /// 获取树型结构
    /// </summary>
    [HttpGet]
    [Route("gettree")]
    public async Task<List<PermissionTreeResponse>> GetTreeAsync()
    {
        return await _permissionAppService.GetTreeAsync();
    }

    /// <summary>
    /// 创建菜单
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("create")]
    public async Task<bool> CreateAsync([FromBody] CreatePermissionRequest request)
    {
        var commandResult = await _mediator.Send(new CreatePermissionCommand()
        {
            Request = request
        });

        return commandResult;
    }
    
    /// <summary>
    /// 删除菜单
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("remove")]
    public async Task<bool> RemoveAsync([FromBody] RemovePermissionRequest request)
    {
        var commandResult = await _mediator.Send(new RemovePermissionCommand()
        {
            Request = request
        });

        return commandResult;
    }
}